package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mylinja 2021-12-22 星期三 10:59
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 提交订单方法
     *
     * @param orders 封装前端数据
     * @return 返回处理结果
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("用户提交订单...");

        ordersService.submitOrder(orders);
        return R.success("下单成功");
    }

    /**
     * 后台查询所有订单
     *
     * @param ordersDto 封装数据
     * @return 返回订单集合
     */
    @GetMapping("/page")
    public R<Page<Orders>> pageFromEmp(OrdersDto ordersDto) {
        log.info("后台查询所有订单...");

        Integer page = ordersDto.getPage();
        Integer pageSize = ordersDto.getPageSize();
        // number是订单号
        String number = ordersDto.getNumber();
        String beginTime = ordersDto.getBeginTime();
        String endTime = ordersDto.getEndTime();

        // SQL: select * from orders where number like (%number%) order_time between (beginTime) and (endTime) limit page, pageSize
        Page<Orders> orderPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> query = new LambdaQueryWrapper<>();
        query.like(number != null, Orders::getNumber, number);
        query.between(beginTime != null, Orders::getOrderTime, beginTime, endTime);
        ordersService.page(orderPage, query);
        return R.success(orderPage);
    }

    /**
     * 用户查询所有订单
     *
     * @param page     页码
     * @param pageSize 每页条目
     * @return 返回订单集合
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> pageFromUser(int page, int pageSize) {
        log.info("用户查询所有订单...");

        Page<OrdersDto> ordersDtoPage = ordersService.selectOrdersFromUser(page, pageSize);
        return R.success(ordersDtoPage);
    }

    /**
     * 修改订单状态
     * @param orders 订单
     * @return 返回修改结果
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        log.info("修改订单状态...");

        // SQL: update orders set status = ? where number = ?
        ordersService.updateById(orders);
        return R.success("更新成功");
    }

    @PostMapping("/again")
    public R<Orders> again(@RequestBody Orders orders){
        log.info("再来一单！！！");

        //1 把上次的订单查询出来，返回回去
        Orders orders1 = ordersService.getById(orders.getId());
        return R.success(orders1);
    }
}
